﻿using MobleWeiXin.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace MobleWeiXin.BussinessData
{
    public class BusinessDataManager
    {
        /// <summary>
        /// 获取最新收录的9条微信
        /// </summary>
        /// <returns></returns>
        public List<WeiXinUserInfoModel> LasterSlList()
        {
            var queryList = new List<WeiXinUserInfoModel>();
            var sql = string.Format("select top(9) Id,WxName,SuccessTime,WxNavId from C_WxUserInfo where InfoStatus=1 order by SuccessTime desc");
            try
            {
                DataTable dt = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        var WeiXinUserInfoModel = new WeiXinUserInfoModel();
                        WeiXinUserInfoModel.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                        WeiXinUserInfoModel.WxNavId = Convert.ToInt32(dt.Rows[j]["WxNavId"]);
                        WeiXinUserInfoModel.WxName = dt.Rows[j]["WxName"].ToString();
                        WeiXinUserInfoModel.SuccessTime = Convert.ToDateTime(dt.Rows[j]["SuccessTime"]).ToLongDateString();
                        queryList.Add(WeiXinUserInfoModel);
                    }
                }
            }
            catch (Exception ex)
            {
              
            }
            return queryList;
        }

        /// <summary>
        /// 获取级别二的列表
        /// </summary>
        /// <returns></returns>
        public List<WeiXinUserInfoModel> Level2List()
        {
            var queryList = new List<WeiXinUserInfoModel>();
            var sql = string.Format("select cw.Id,cw.WxName,cw.WxQrCode,cw.SmallIntroduce,cw.ReadCount from C_WxOrder co join C_WxUserInfo cw on co.IsPublic=1 and co.PayLevel =3 and co.ExpiredTime>=GETDATE() and co.WxUserInfoId = cw.Id and cw.InfoStatus=1");
            try
            {
                DataTable dt = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        var WeiXinUserInfoModel = new WeiXinUserInfoModel();
                        WeiXinUserInfoModel.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                        WeiXinUserInfoModel.WxName = dt.Rows[j]["WxName"].ToString();
                        WeiXinUserInfoModel.WxQrCode = dt.Rows[j]["WxQrCode"].ToString();
                        WeiXinUserInfoModel.SmallIntroduce = dt.Rows[j]["SmallIntroduce"].ToString();
                        WeiXinUserInfoModel.ReadCount = Convert.ToInt32(dt.Rows[j]["ReadCount"].ToString());
                        queryList.Add(WeiXinUserInfoModel);
                    }
                }
            }
            catch (Exception ex)
            {
            
            }
            return queryList;
        }
        public List<WeiXinNavItem> GetWxNavList()
        {
            var result = new List<WeiXinNavItem>();
            var sql = "select * from C_WxNav where IsTrue=1";
            try
            {
                DataTable dt = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        var nav = new WeiXinNavItem();
                        nav.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                        nav.TypeName = dt.Rows[j]["TypeName"].ToString();

                        result.Add(nav);
                    }
                }
            }
            catch (Exception ex)
            {
        
            }
            return result;
        }

        /// <summary>
        /// 分类导航,每个分类首先取付费的，不够8个，继续按时间先后顺序取剩下的
        /// </summary>
        /// <returns></returns>
        public Dictionary<WeiXinNavItem, List<WeiXinUserInfoModel>> Leva1Dic()
        {
            var result = new Dictionary<WeiXinNavItem, List<WeiXinUserInfoModel>>();
            List<WeiXinNavItem> navList = this.GetWxNavList();
            var sql = "";
            try
            {
                foreach (var nav in navList)
                {
                    sql = string.Format("select top(8) cw.Id,cw.WxName from C_WxOrder co join C_WxUserInfo cw on co.IsPublic=1 and co.PayLevel =2 and co.ExpiredTime>=GETDATE() and co.WxUserInfoId = cw.Id and cw.InfoStatus=1 and cw.WxNavId = {0}", nav.Id);
                    DataTable dt = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql).Tables[0];
                    var _tempList = new List<WeiXinUserInfoModel>();
                    if (dt.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            var WeiXinUserInfoModel = new WeiXinUserInfoModel();
                            WeiXinUserInfoModel.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                            WeiXinUserInfoModel.WxName = dt.Rows[j]["WxName"].ToString();
                            _tempList.Add(WeiXinUserInfoModel);
                        }
                    }
                    var passCount = 8 - _tempList.Count;
                    if (passCount > 0)
                    {
                        //差的从免费里面取
                        var sql2 = string.Format("select top(15) Id,WxName from C_WxUserInfo where InfoStatus=1 and WxNavId = {0}", nav.Id);
                        DataTable dt2 = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql2).Tables[0];
                        if (dt2.Rows.Count > 0)
                        {
                            for (int z = 0; z < dt2.Rows.Count; z++)
                            {
                                if (!_tempList.Any(i => i.Id == Convert.ToInt32(dt2.Rows[z]["Id"])))//防止重复，在sql里过滤重复，性能差，前端绑定的时候，取前8个
                                {
                                    var WeiXinUserInfoModel2 = new WeiXinUserInfoModel();
                                    WeiXinUserInfoModel2.Id = Convert.ToInt32(dt2.Rows[z]["Id"]);
                                    WeiXinUserInfoModel2.WxName = dt2.Rows[z]["WxName"].ToString();
                                    _tempList.Add(WeiXinUserInfoModel2);
                                }
                            }
                        }
                    }
                    result.Add(nav, _tempList);

                }
            }
            catch (Exception ex)
            {
                result = new Dictionary<WeiXinNavItem, List<WeiXinUserInfoModel>>();
           
            }
            return result;

        }


        /// <summary>
        /// 获取最新N篇资讯
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<WeiXinNewsModel> GetLasterNews(int count)
        {
            var sql = "select top(" + count + ") Id,WxNewsName from C_WxNews where IsPublic = 1 order by Id desc";
            var queryList = new List<WeiXinNewsModel>();
            try
            {

                DataTable dt = new SqlManager("ConnectionStrings_WeiXin").ExecuteDataset(CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        var WeiXinNewsModel = new WeiXinNewsModel();
                        WeiXinNewsModel.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                        WeiXinNewsModel.WxNewsName = dt.Rows[j]["WxNewsName"].ToString();
                        queryList.Add(WeiXinNewsModel);
                    }
                }
            }
            catch (Exception ex)
            {
     
            }
            return queryList;
        }

        /// <summary>
        /// 获取热门文章或者视频，1文章，2视频
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<ArticleListItem> GetHotArticleOrVideo(int type)
        {
            string sql = "";
            var result = new List<ArticleListItem>();
            if (type == 1)
            {
                sql = string.Format("select top(6) a.Id,a.Title,s.ReadCount,s.CommentCount,a.ImgUrl from C_Article a join C_ArticleStatistics s on a.Id=s.ArticleId and a.IsVideo={0} order by s.RealReadCount desc", 0);
            }
            else
            {
                sql = string.Format("select top(6) a.Id,a.Title,s.ReadCount,s.CommentCount,a.ImgUrl from C_Article a join C_ArticleStatistics s on a.Id=s.ArticleId and a.IsVideo={0} order by s.RealReadCount desc", 1);
            }
            try
            {
                DataTable dt = new SqlManager().ExecuteDataset(CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        var articleListItem = new ArticleListItem();
                        articleListItem.Id = Convert.ToInt32(dt.Rows[j]["Id"]);
                        articleListItem.ImgUrl = dt.Rows[j]["ImgUrl"].ToString();
                        articleListItem.Title = dt.Rows[j]["Title"].ToString();
                        result.Add(articleListItem);
                    }
                }
            }
            catch (Exception ex)
            {

               
            }

            return result;
        }

    }
}